package com.ruoyi.web.controller.system;

import java.util.List;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysTable;
import com.ruoyi.system.service.ISysTableService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 档案设置Controller
 *
 * @author ruoyi_hsl
 * @date 2022-02-15
 */
@RestController
@RequestMapping("/system/table")
public class SysTableController extends BaseController
{
    @Autowired
    private ISysTableService sysTableService;

    /**
     * 查询档案设置列表
     */
    @PreAuthorize("@ss.hasPermi('system:table:list')")
    @ApiOperation("查询档案设置列表")
    @GetMapping("/list")
    public TableDataInfo list(SysTable sysTable)
    {
        startPage();
        List<SysTable> list = sysTableService.selectSysTableList(sysTable);
        return getDataTable(list);
    }

    /**
     * 导出档案设置列表
     */
    @PreAuthorize("@ss.hasPermi('system:table:export')")
    @Log(title = "档案设置", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(SysTable sysTable)
    {
        List<SysTable> list = sysTableService.selectSysTableList(sysTable);
        ExcelUtil<SysTable> util = new ExcelUtil<SysTable>(SysTable.class);
        return util.exportExcel(list, "table");
    }

    /**
     * 获取档案设置详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:table:query')")
    @ApiOperation("获取档案设置详细信息")
    @ApiImplicitParam(name = "recNo", value = "档案设置编号", required = true, dataType = "String", paramType = "传档案设置编号")
    @GetMapping(value = "/get/{recNo}")
    public AjaxResult getInfo(@PathVariable("recNo") String recNo)
    {
        return AjaxResult.success(sysTableService.selectSysTableById(recNo));
    }

    /**
     * 新增档案设置
     */
    @PreAuthorize("@ss.hasPermi('system:table:add')")
    @Log(title = "档案设置", businessType = BusinessType.INSERT)
    @ApiOperation("新增档案设置")
    @ApiImplicitParam(name = "sysTable", value = "档案设置对象", required = true, dataType = "SysTable", paramType = "传档案设置对象")
    @PostMapping
    public AjaxResult add(@RequestBody SysTable sysTable)
    {
        String regex = "[a-zA-Z_]{4,16}";

        boolean flag = sysTable.getTableName().matches(regex);
        if(flag){

        }else {
            return AjaxResult.error("表名格式不对");
        }
        //不合法
        return toAjax(sysTableService.insertSysTable(sysTable));
    }

    /**
     * 修改档案设置
     */
    @PreAuthorize("@ss.hasPermi('system:table:edit')")
    @Log(title = "档案设置", businessType = BusinessType.UPDATE)
    @ApiOperation("修改档案设置")
    @ApiImplicitParam(name = "sysTable", value = "档案设置对象", required = true, dataType = "SysTable", paramType = "传档案设置对象")
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody SysTable sysTable)
    {
        return sysTableService.updateSysTable(sysTable);
    }

    /**
     * 删除档案设置
     */
    @PreAuthorize("@ss.hasPermi('system:table:remove')")
    @Log(title = "档案设置", businessType = BusinessType.UPDATE)
    @ApiOperation("删除档案设置")
    @ApiImplicitParam(name = "recNos", value = "档案设置编号数组", required = true, dataType = "String[]", paramType = "传档案设置编号数组")
    @GetMapping("/{recNos}")
    public AjaxResult remove(@PathVariable String[] recNos)
    {
        return toAjax(sysTableService.deleteSysTableByIds(recNos));
    }

    /**
     * 查询档案设置非临时表的列表
     */
    @PreAuthorize("@ss.hasPermi('system:table:list')")
    @ApiOperation("查询档案设置非临时表的列表")
    @ApiImplicitParam(name = "sysTable", value = "档案设置对象", required = true, dataType = "SysTable", paramType = "传档案设置对象")
    @GetMapping("/nonTemporary")
    public TableDataInfo nonTemporary(SysTable sysTable)
    {
        startPage();
        List<SysTable> list = sysTableService.nonTemporary(sysTable);
        return getDataTable(list);
    }

}
